/*
 *  Netzpult - Your desk on the web
 *  Copyright (C) 2011  Ulrich Hilger, http://uhilger.de
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/
 */

package de.uhilger.netzpult.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * User interface component to log in to Audio Hub
 *
 * @author Copyright (c) Ulrich Hilger, http://uhilger.de
 * @author Published under the terms and conditions of
 * the <a href="http://www.gnu.org/licenses/" target="_blank">GNU General Public License</a>
 */
public class LoginDialog extends DialogBox implements ClickHandler {

  private TextBox userName;
  private PasswordTextBox userPw;
  private Button commitBtn;
  private Button cancelBtn;
  private boolean result;
  private LoginDialogListener listener;
  
  public LoginDialog(String title, LoginDialogListener listener) {
    super();
    this.listener = listener;
    buildUi(title);
  }
  
  /**
   * Create a new object of class LoginDialog
   * @param title  the title of this dialog
   */
  public LoginDialog(String title) {
  	this(title, null);
  }

  /**
   * Determine whether or not the user did press the 'OK' button
   * @return  true when the user pressed the 'OK' button, false if not
   */
  public boolean didSave() {
    return result;
  }

  /**
   * Get the user name entered into this login dialog
   * @return  the user name
   */
  public String getUserName() {
    return userName.getText();
  }

  /**
   * Get the password entered into this login dialog
   * @return  the password
   */
  public String getUserPw() {
    return userPw.getText();
  }

  /**
   * Build the user interface of this login dialog
   * @param title
   */
  private void buildUi(String title) {
    userName = new TextBox();
    userPw = new PasswordTextBox();

    Grid grid = new Grid(2, 2);
    grid.setWidget(0, 0, new Label("Benutzerkennung: "));
    grid.setWidget(0, 1, userName);
    grid.setWidget(1, 0, new Label("Kennwort: "));
    grid.setWidget(1, 1, userPw);

    cancelBtn = new Button("Abbrechen");
    cancelBtn.addClickHandler(this);
    commitBtn = new Button("Anmelden");
    commitBtn.addClickHandler(this);

    HorizontalPanel buttonPanel = new HorizontalPanel();
    buttonPanel.add(cancelBtn);
    buttonPanel.add(commitBtn);

    VerticalPanel mainPanel = new VerticalPanel();

    mainPanel.add(grid);
    mainPanel.add(buttonPanel);

    add(mainPanel);

    setAnimationEnabled(true);
    setTitle(title);
    setText(title);

  	userName.setFocus(true);
    //center();
  }

  public void initFocus() {
  	userName.setEnabled(true);
  	userName.setTabIndex(1);
  	userPw.setTabIndex(2);
  	userPw.setEnabled(true);
  	cancelBtn.setTabIndex(3);
  	cancelBtn.setEnabled(true);
  	commitBtn.setTabIndex(4);
  	commitBtn.setEnabled(true);
  	userName.setFocus(true);
  }
  
  /**
   * Handle the click of the mouse onto a part of this user interface component
   * @param event  the mouse event to handle
   */
  public void onClick(ClickEvent event) {
    Object src = event.getSource();
    if(src.equals(cancelBtn)) {
      result = false;
      if(listener != null) {
      	listener.loginCancelled();
      }
    } else if(src.equals(commitBtn)) {
      result = true;
    }
    this.hide();
  }
  
  public interface LoginDialogListener {
  	public void loginCancelled();
  }

}
